package com.groupfifteen.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.groupfifteen.dao.SysTeacherDao;
import com.groupfifteen.entity.SysTeacherEntity;
import com.groupfifteen.vo.SysTeacherVo;
import org.springframework.stereotype.Service;

@Service("sysTeacherService")
public class SysTeacherServiceImpl extends ServiceImpl<SysTeacherDao, SysTeacherEntity>
        implements SysTeacherService {
    @Override
    public Integer AddSysTeacher(SysTeacherEntity sysTeacherEntity) {

        return baseMapper.insert(sysTeacherEntity);
    }
    @Override
    public Integer DeleteSysTeacher(Integer teacherId) {
        return baseMapper.deleteById(teacherId);
    }

    @Override
    public SysTeacherEntity GetSysTeacher(Integer teacherId) {
        return baseMapper.selectById(teacherId);
    }

    @Override
    public  Integer UpdateSysTeacher(SysTeacherEntity sysTeacherEntity){
        return baseMapper.updateById(sysTeacherEntity);
    }

    @Override
    public IPage<SysTeacherEntity> Query(SysTeacherEntity sysTeacherEntity, int pageNo, int pageSize) {

        LambdaQueryWrapper<SysTeacherEntity> query = Wrappers.lambdaQuery();
        query.eq(sysTeacherEntity.getTeacherId() != null, SysTeacherEntity::getTeacherId, sysTeacherEntity.getTeacherId());

        if( pageNo == 0 )
        {
            pageNo = 1;
        }
        if( pageSize == 0 ){
            pageSize = 10;
        }

        IPage<SysTeacherEntity> page = Page.of(pageNo, pageSize);

        return baseMapper.selectPage(page, query);
    }

    @Override
    public IPage<SysTeacherEntity> Query(SysTeacherVo sysTeacherVo) {
        LambdaQueryWrapper<SysTeacherEntity> query = Wrappers.lambdaQuery();
        query.eq(sysTeacherVo.getTeacherId() != null, SysTeacherEntity::getTeacherId, sysTeacherVo.getTeacherId())
                .eq(sysTeacherVo.getTeacherName() != null, SysTeacherEntity::getTeacherName, sysTeacherVo.getTeacherName())
                .eq(sysTeacherVo.getPwd() != null, SysTeacherEntity::getPwd, sysTeacherVo.getPwd());

        int currentPage = sysTeacherVo.getCurrentPage();
        int pageSize = sysTeacherVo.getPageSize();
        IPage<SysTeacherEntity> page = new Page<>(currentPage, pageSize);

        return baseMapper.selectPage(page, query);
    }


}
